package com.myz.rabbitmq.study5;

import com.myz.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

// 消费者1
public class ReceiveLogs01 {
    // 交换机的名字
    public static final String EXCHANGE_NAME = "logs";
    public static void main(String[] args) throws Exception {
        // 获取信道 channel
        Channel channel = RabbitMqUtils.getChannel();
        // 声明一个交换机 交换机类型为广播类型 fanout
        channel.exchangeDeclare(EXCHANGE_NAME,"fanout");
        // 声明一个临时队列
        /*
         * 生成一个临时队列，队列的名称时随机的
         * 当消费者断开与队列的连接时，队列就自动删除
         * */
        String queue = channel.queueDeclare().getQueue();
        /* 绑定交换机与队列
         *   参数1:队列名 参数2:交换机名 参数3:routingKey
         * */
        channel.queueBind(queue,EXCHANGE_NAME,"");
        System.out.println("ReceiveLogs01等待接收消息，把接收到的消息打印在屏幕上...");
        DeliverCallback deliverCallback = (consumerTag, message)->{
            System.out.println("ReceiveLogs01接收消息:"+new String(message.getBody()));
        };
        CancelCallback cancelCallback = (consumerTag)->{
            System.out.println("取消接受消息的编号:"+consumerTag);
        };
        channel.basicConsume(queue,true,deliverCallback,cancelCallback);
    }
}
